﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace 高级控件演示.ViewModels
{
    public class PrintContentViewModel:BindableBase
    {
        private DispatcherTimer _timer;
        private int _currentIndex;
        private readonly string _fullText = "坦克,颜色:雨点,雄鸽,体重:2.2斤\r\n" +
                                            "芷儿凤凰鸽舍镇棚鸽";

        public DelegateCommand StartTypingCommand { get; private set; }
        

        private string displayText;

        public string DisplayText
        {
            get { return displayText; }
            set { displayText = value;RaisePropertyChanged(); }
        }

        public PrintContentViewModel()
        {
            StartTypingCommand = new DelegateCommand(StartTypingCommandFunc);

            _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
            _timer.Tick += (s, e) => TypeNextCharacter();
        }

        private void StartTypingCommandFunc()
        {
            DisplayText = "";
            _currentIndex = 0;
            _timer.Start();
        }

        private void TypeNextCharacter()
        {
            if (_currentIndex < _fullText.Length)
            {
                DisplayText += _fullText[_currentIndex];
                _currentIndex++;
            }
            else
            {
                _timer.Stop();
            }
        }
    }
}
